package com.example.myapplication

import android.content.Intent
import android.icu.text.Transliterator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.example.myapplication.model.Student
import com.example.myapplication.adapter.StudentAdapter
import com.example.myapplication.service.StudentService
import com.example.myapplication.utils.HttpUtil
import com.example.myapplication.utils.ServerCreator
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlinx.android.synthetic.main.activity_fourth.*
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import java.io.IOException

class FourthActivity : AppCompatActivity() {
    private val studentList=ArrayList<Student>()
//    var students:List<Student>?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fourth)
val activty=this
        val studentService= ServerCreator.create<StudentService>();
//2. 调用实例对象方法，由于需要网络请求，需要继续调用enqueue方法
        studentService.getStudentData().enqueue(object: retrofit2.Callback<List<Student>> {
            override fun onFailure(call: retrofit2.Call<List<Student>>, t: Throwable) {
                t.printStackTrace()
            }
            //3. 请求成功！
            override fun onResponse(
                call: retrofit2.Call<List<Student>>,
                response: retrofit2.Response<List<Student>>
            ) {
                var students = response.body()
                if (students != null) {
                    val adapter = StudentAdapter(activty, R.layout.student_item, studentList)
                    list_view.adapter = adapter
                    for (index in students){
                    studentList.add(Student(index.stuNo,index.stuName,index.classes,index.gender,index.department,index.tel,index.dormNo,index.photoPath))
                    }

                list_view.setOnItemClickListener{
                    parent,view,position,id->
                    val student= students[position]
                    val intent=Intent(activty,SixthActivity::class.java)
                    intent.putExtra("stuno",student.stuNo)
                    intent.putExtra("stuname",student.stuName)
                    intent.putExtra("classes",student.classes)
                    intent.putExtra("department",student.department)
                    intent.putExtra("tel",student.tel)
                    intent.putExtra("dormno",student.dormNo)
                    intent.putExtra("gender",student.gender)
                    intent.putExtra("photopath",student.photoPath)
                    startActivity(intent)
                }
                }else{
                    Log.d("FourthActivity","发生错误啦" )
                }
            }
        })
}
}


//initStudent()
//        val adapter=StudentAdapter(this,R.layout.student_item,studentList)
//        list_view.adapter=adapter
//        list_view.setOnItemClickListener{
//            parent,view,position,id->
//            val student=students[position]
//            val intent= Intent(this,ThirdActivity::class.java)
//            intent.putExtra("stuNo",student.stuNo)
//            intent.putExtra("stuName",student.stuName)
//            intent.putExtra("classes",student.classes)
//            intent.putExtra("gender",student.gender)
//            intent.putExtra("department",student.department)
//            intent.putExtra("tel",student.tel)
//            intent.putExtra("dormNo",student.dormNo)
//            intent.putExtra("photoPath",student.photoPath)
//            startActivity(intent)
//        }
